﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Passwords.Pages;
using Passwords.Models;
using Silvermoon.Controls;
using Silvermoon.Windows.Styles;
using Passwords.Utils;
using Silvermoon.Navigation;
using Passwords.Biz;

namespace Passwords.Controllers
{
    public static class DetailControllerHelper
    {
        public static ListViewController<NameValueItem, PasswordItem> InitializeView(ListView listView, Password password, bool isReadOnly)
        {
            var controller = new ListViewController<NameValueItem, PasswordItem>(listView);
            SetStyles(controller);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<NameValueItem>>(controller_UpdateItem);

            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<NameValueItem>>(controller_InitializeItemReadonly);
            if (password != null) controller.DataSource = password.GetItemsToView();
            return controller;
        }

        static void controller_InitializeItemReadonly(object sender, ItemControlGeneratorEventArgs<NameValueItem> e)
        {
            e.Item.IsReadonly = true;
        }


        private static void SetStyles(ListViewController<NameValueItem, PasswordItem> controller)
        {
            controller.ItemStyle = new NameValueItemStyle { NameFontScaleFactor = 1f, ValueFont = FontCache.MediumRegularFont, Margin = new Margin(0, 0, 0, 24) };
        }

        static void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<NameValueItem> e)
        {
            NameValueItem item = e.Item;
            PasswordItem data = e.Value as PasswordItem;

            item.Name = data.Name;
            item.Value = data.Value;
        }
    }
}
